/**
 * @file lv_obj_matrix_2048.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lvgl.h"
#include "lv_obj_matrix.h"
#include "xgame_2048.h"
/*********************
 *      DEFINES
 *********************/

 #ifndef XGAME_2048_ROW_MAX
  #define XGAME_2048_ROW_MAX   4
#endif

#ifndef XGAME_2048_COL_MAX
  #define XGAME_2048_COL_MAX   4
#endif

#ifndef XGAME_2048_WIN_MAX
  #define XGAME_2048_WIN_MAX   2048
#endif

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void key_event_handler(lv_event_t * e);
static void create_cell_cb(lv_obj_t * matrix, uint16_t hpos, uint16_t vpos);
static void event_cell_cb(lv_obj_t *matrix, lv_event_code_t event_code, int16_t hpos, int16_t vpos);

 /**********************
 *  STATIC VARIABLES
 **********************/

/**<Matrix object distance from outer frame margin */
static const struct _lv_obj_matrix_info_t info_auto_size = {
    create_cell_cb, NULL, event_cell_cb,
    {8, 8, 8, 8},
    {0, 0},     /**< the size of the cell container, 0:auto max size */
    XGAME_2048_ROW_MAX,          /**< Number of horizontal objects */
    XGAME_2048_COL_MAX,          /**< Number of vertical objects */
    0,                     /**< when hcount or vcount is 0, max count*/
    LV_ALIGN_CENTER,       /**< align */
};

/*game config*/
struct _xgame_2048cfg  _2048cfg = {
  XGAME_2048_ROW_MAX,
  XGAME_2048_COL_MAX,
  XGAME_2048_WIN_MAX,
};

struct _xgame_2048 game;

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_obj_matrix_2048(void)
{
    lv_obj_t * obj_matrix = lv_obj_matrix_create(lv_screen_active());
    lv_obj_matrix_set_info(obj_matrix, &info_auto_size);
    lv_obj_matrix_update(obj_matrix);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

static void key_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {


    }
}

static void create_cell_cb(lv_obj_t * matrix, uint16_t hpos, uint16_t vpos)
{
    lv_obj_t * cell_container = lv_obj_matrix_get_cell_container(matrix, hpos, vpos);

    lv_obj_t * button = lv_button_create(cell_container);
    lv_obj_set_size(button, LV_PCT(90), LV_PCT(90));
    lv_obj_set_align(button, LV_ALIGN_CENTER);
    lv_obj_t  *label = lv_label_create(button);
    char buf[4];
    buf[0] = vpos + '1'; buf[1] = '-'; buf[2] = hpos + '1';  buf[3] = '\0';
    lv_label_set_text(label, buf);
    lv_obj_set_align(label, LV_ALIGN_CENTER);
}

static void event_cell_cb(lv_obj_t *matrix, lv_event_code_t event_code, int16_t hpos, int16_t vpos)
{
    if((hpos < 0) || (vpos < 0)) return;
    lv_obj_t * cell_container = lv_obj_matrix_get_cell_container(matrix, hpos, vpos);
    if(cell_container == NULL) return;
    lv_obj_t * label = lv_obj_get_child(lv_obj_get_child(cell_container, 0), 0);
    char buf[4];
    buf[0] = vpos + '1'; buf[1] = '-'; buf[2] = hpos + '1';  buf[3] = '\0';
    lv_label_set_text(label, buf);
    lv_obj_invalidate(label);
}
